package model

import (
	"fmt"

	"gorm.io/gorm"
)

type Adopt struct {
	AdoptID      int           `gorm:"primary_key;autoIncrement"`
	PassageID    int            `gorm:"not null"`
	ApplyID      int            `gorm:"not null"`
	PromoterID   int            `gorm:"not null"`
	//1-申请中；  3-同意领养； 4-领养成功； 5-双方评价； -1-拒绝领养 -2-领养失败
	AdoptProcess int            `gorm:"not null"`
	Reason       string         `gorm:"not null"`
	DeletedAt    gorm.DeletedAt `gorm:"index"`
	Passage      *Passage       `gorm:"foreignKey:PassageID"`
	User1        *User          `gorm:"foreignKey:ApplyID"`
	User2        *User          `gorm:"foreignKey:PromoterID"`
}


type UserAdopt struct {
	AdoptID int `json:"adoptID"`
	ApplyUserID int `json:"applyuserID"`
	ApplyUName  string `json:"applyuName"`
	ApplyProfilePhoto string `json:"applyprofilePhoto"`
	PromoteUserID int `json:"promoteuserID"`
	PromoteUName  string `json:"promoteuName"`
	PromoteProfilePhoto string `json:"promoteprofilePhoto"`
	PassageID int `json:"passageID"`
	Process int `json:"process"`
}
func (a *Adopt) SaveAdoptApplication() (*Adopt, error) {
	err := DB.Create(a).Error
	if err != nil {
		fmt.Println("Error in saving adopt application")
		return nil, err
	}
	//生成记录
	return a, nil
}
func (a* Adopt) UpdateAdoptApplication() (*Adopt, error){
	err := DB.Save(a).Error
	if err != nil {
		fmt.Println("Error in updating adopt application")
		return nil, err
	}
	return a, nil
}

func FindAdoptApplication(passageID int, applyID int, promoterID int) (*Adopt, error) {
	var adopt Adopt
	err := DB.Model(&Adopt{}).Where("PassageID = ? and ApplyID = ? and PromoterID = ?", passageID, applyID, promoterID).First(&adopt).Error
	if err != nil {
		fmt.Println("Error in finding adopt application")
		return nil, err
	}
	return &adopt, nil
}

func FindAdoptApplicationByID(adoptID int) (*Adopt, error) {
	var adopt Adopt
	err := DB.Model(&Adopt{}).Where("AdoptID = ?", adoptID).First(&adopt).Error
	if err != nil {
		fmt.Println("Error in finding adopt application by ID")
		return nil, err
	}
	return &adopt, nil

}

func FindAdoptStatus(passageID int,userID int) (*Adopt, error) {
	var adopt []Adopt
	err := DB.Model(&Adopt{}).Where("PassageID = ? ", passageID).Find(&adopt).Error
	if err != nil {
		fmt.Println("Error in finding adopt status")
		return nil, err
	}
	var adopt_finished Adopt
	err = DB.Model(&Adopt{}).Where("PassageID = ? and AdoptProcess>=4 ", passageID).Find(&adopt_finished).Error

	//先判断是否已经有人领养成功
	if adopt_finished.AdoptID != 0{
		return &adopt_finished, nil
	}
	//判断该用户自己是否申请了这个帖子的领养
	var selfadopt []Adopt
	err = DB.Model(&Adopt{}).Where("PassageID = ? and ApplyID = ?",passageID,userID).Find(&selfadopt).Error
	//自己没有申请记录
	if len(selfadopt) ==0{
		return nil , nil
	}
	//自己有申请记录
	for _, a := range selfadopt{
		//判断申请记录是否都是拒绝领养
		if a.AdoptProcess>0{
			return &a, nil
		}
	}
	//都是领养失败的
	return nil,nil
}

func FindUserAdoptApplication(userID int) ([]UserAdopt, error) {
	var adopt []Adopt
	err := DB.Model(&Adopt{}).Where("PromoterID = ? or ApplyID = ?", userID, userID).Order("AdoptID desc").Find(&adopt).Error
	if err != nil {
		fmt.Println("Error in finding user promote adopt application")
		return nil, err
	}

	var result []UserAdopt
	for _, a := range adopt {
		user1,_:=GetUserMiniByID(a.ApplyID)
		user2,_:=GetUserMiniByID(a.PromoterID)
		result = append(result, UserAdopt{
			ApplyUserID: user1.UserID,
			ApplyUName: user1.UName,
			ApplyProfilePhoto: user1.ProfilePhoto,
			PromoteUserID: user2.UserID,
			PromoteUName: user2.UName,
			PromoteProfilePhoto: user2.ProfilePhoto,
			AdoptID: a.AdoptID,
			PassageID: a.PassageID,
			Process: a.AdoptProcess,
		})
		
	}
	return result, nil
}
